//
//  FEJokeTableViewController.swift
//  JokeClient
//
//  Created by hryan on 15/5/21.
//  Copyright (c) 2015年 hryan. All rights reserved.
//

enum FEJokeTableViewControllerType : Int {
    case HotJoke
    case NewestJoke
    case ImageTruth
}

import UIKit

class FEJokeTableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,FERefreshViewDelegate

{
    let identifier = "cell"
    var jokeType : FEJokeTableViewControllerType = .HotJoke
    var tableView : UITableView?
    var dataArray = NSMutableArray()
    var page :Int = 1
    var refreshView :FERefreshView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "imageViewTapped", name: "imageViewTapped", object: nil)
    }
    
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: "imageViewTapped", object: nil)
    }
    
    func setupViews()
    {
        var width = self.view.frame.size.width
        var height = self.view.frame.size.height
        self.tableView = UITableView(frame: CGRectMake(0, 64, width, height-64))
        self.tableView?.dataSource = self
        self.tableView?.delegate = self
        self.tableView?.separatorStyle = UITableViewCellSeparatorStyle.None
        var nib = UINib(nibName: "FEJokeTableViewCell", bundle: nil)
        
        self.tableView?.registerNib(nib, forCellReuseIdentifier: identifier)
        var arr = NSBundle.mainBundle().loadNibNamed("FERefreshView", owner: self, options: nil) as Array
        self.refreshView = arr[0] as? FERefreshView
        self.refreshView!.delegate = self
        self.tableView?.tableFooterView = self.refreshView
        self.view.addSubview(self.tableView!)
        
    }
    
    func loadData()
    {
        var url = urlString()
        self.refreshView!.startLoading()
        
        
    }
    
    func urlString()->String
    {
        if jokeType == .HotJoke
        {
            return "http://m2.qiushibaike.com/article/list/suggest?count=20&page=\(page)"
        }
        else if jokeType == .NewestJoke //最新糗事
        {
            return "http://m2.qiushibaike.com/article/list/latest?count=20&page=\(page)"
        }
        else//有图有真相
        {
            return "http://m2.qiushibaike.com/article/list/imgrank?count=20&page=\(page)"
        }
        
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as? UITableViewCell
        var index = indexPath.row
//        var data = self.dataArray[index] as! NSDictionary
//        cell.data = data;
        return cell!
    }
    
    func refreshView(refreshView: FERefreshView, didClickButton btn: UIButton) {
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    
}
